<?php
class Admin_Models_tblImages extends Libs_Model {

    protected $queryUnit;

    public function __construct() {
        parent::__construct();
        $this->queryUnit = new Libs_QueryUnit();
    }

    private $img_id;
    private $app_id;
    private $url;

    /**
     * @param mixed $img_id
     */
    public function setImgId($img_id) {
        $this->img_id = $img_id;
    }

    /**
     * @return mixed
     */
    public function getImgId() {
        return $this->img_id;
    }

    /**
     * @param mixed $app_id
     */
    public function setAppId($app_id) {
        $this->app_id = $app_id;
    }

    /**
     * @return mixed
     */
    public function getAppId() {
        return $this->app_id;
    }

    /**
     * @param mixed $url
     */
    public function setUrl($url) {
        $this->url = $url;
    }

    /**
     * @return mixed
     */
    public function getUrl() {
        return $this->url;
    }

    public function setCustomerValue($row, $isKey = TRUE) {
        $img= new Admin_Models_tblImages();
        if($isKey){
            $img->setImgId($row['img_id']);
        }
        $img->setAppId($row['app_id']);
        $img->setUrl($row['url']);
        return $img;
    }
    public function getColumnAndValue(Admin_Models_tblImages $img, $isKey= FALSE){
        $arr_data= array();
        if($isKey){
            $arr_data["img_id"]= $img->getImgId();
        }
        $arr_data["app_id"]= $img->getAppId();
        $arr_data["url"]= $img->getUrl();
        return $arr_data;
    }

    public function getAllImageByIDApp($app_id) {
        $listImg = array();
        $execute = $this->queryUnit->getSelect('tbl_images', " app_id= '$app_id'");
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $listImg[]=$this->setCustomerValue($row);
            }
        }
        return $listImg;
    }


    public function insertImg(Admin_Admin_Models_tblImages $img) {
        $execute = $this->queryUnit->getInsert('tbl_images', $this->getColumnAndValue($img));
        if ($execute) {
            return TRUE;
        }
        return FALSE;
    }

    public function updateImg(Admin_Admin_Models_tblImages $img) {
        $execute = $this->queryUnit->getUpdate('tbl_images', $this->getColumnAndValue($img, TRUE), "img_id='" . $img->getImgId() . "'");
        if ($execute) {
            return TRUE;
        }
        return FALSE;
    }

    public function deleteImg($img_id) {
        $execute = $this->queryUnit->getDelete('tbl_images', "img_id='" . $img_id . "'");
        if ($execute) {
            return TRUE;
        }
        return FALSE;
    }

    public function getImgByID($img_id) {
        $img = null;
        $execute = $this->queryUnit->getSelect('tbl_images', " img_id= '$img_id'");
        if (mysql_num_rows($execute) > 0) {
            $row = mysql_fetch_assoc($execute);
            $img= $this->setCustomerValue($row);
        }
        return $img;
    }
    public function delListImg($listID){
        $condition = ' img_id  IN (' . $listID . ')';
        $exxcute= $this->queryUnit->getDelete('tbl_images', $condition);
        if($exxcute){
            return TRUE;
        }
        return FALSE;
    }
    public function numImgByAppID($app_id){
        $execute= $this->queryUnit->executeQuery("SELECT COUNT(*) as total FROM `tbl_images` WHERE `app_id`=".$app_id);
        $numImg= mysql_fetch_assoc($execute);
        return  $numImg['total'];
    }

}